package com.web.netty.thread;

import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author: wws
 * @Date: 2021/1/5 12:12
 * describe:
 */
@Component
public class NettyOutThread implements Runnable {

    @Qualifier("getOutQueue")
    @Autowired
    public LinkedBlockingQueue<byte[]> outQueue;

    @Autowired
    public ConcurrentMap<String, Channel> channelMap;

    private Thread thread;

    @Autowired
    private String gateWayBridgeIp;



    Channel channel;

    @PostConstruct
    public void init() {
        thread = new Thread(this);
        thread.start();

    }

    @Override
    public void run() {
//        synchronized (channelMap) {
//            try
//            {
//                channelMap.wait();
//                channel = channelMap.get(gateWayBridgeIp);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        }

        for (; ; ) {
            try {
                byte[] downPayload = outQueue.take();
                channelMap.get(gateWayBridgeIp).writeAndFlush(Unpooled.copiedBuffer(downPayload));
                System.out.println("down msg!!!!!!!!!!!!!!!!!");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }
}
